<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { userExists, updatePassword } from '../services/UserService.js'

// 表单数据
const username = ref('')
const newPassword = ref('')
const confirmNewPassword = ref('')
const error = ref('')
const success = ref('')
const router = useRouter()

// 验证用户名格式
const validateUsername = (username) => {
  // 用户名至少3个字符，不超过20个字符
  if (username.length < 3 || username.length > 20) {
    return '用户名长度必须在3-20个字符之间'
  }
  // 用户名只能包含字母、数字和下划线
  if (!/^[a-zA-Z0-9_]+$/.test(username)) {
    return '用户名只能包含字母、数字和下划线'
  }
  return ''
}

// 验证密码格式
const validatePassword = (password) => {
  // 密码至少6个字符
  if (password.length < 6) {
    return '密码长度至少为6个字符'
  }
  // 密码强度检查
  const hasLetter = /[a-zA-Z]/.test(password)
  const hasNumber = /[0-9]/.test(password)
  if (!hasLetter || !hasNumber) {
    return '密码必须包含字母和数字'
  }
  return ''
}

// 处理密码重置
const handleResetPassword = (e) => {
  e.preventDefault()
  
  // 重置消息
  error.value = ''
  success.value = ''
  
  // 简单的表单验证
  if (!username.value || !newPassword.value || !confirmNewPassword.value) {
    error.value = '请填写所有必填字段'
    return
  }
  
  // 验证用户名格式
  const usernameError = validateUsername(username.value)
  if (usernameError) {
    error.value = usernameError
    return
  }
  
  // 验证密码格式和强度
  const passwordError = validatePassword(newPassword.value)
  if (passwordError) {
    error.value = passwordError
    return
  }
  
  // 检查两次输入的密码是否一致
  if (newPassword.value !== confirmNewPassword.value) {
    error.value = '两次输入的新密码不一致'
    return
  }
  
  // 检查用户是否存在
  if (!userExists(username.value)) {
    error.value = '该用户名不存在'
    return
  }
  
  // 调用用户服务更新密码
  const result = updatePassword(username.value, newPassword.value)
  
  if (result.success) {
    success.value = result.message
    // 3秒后重定向到登录页
    setTimeout(() => {
      router.push('/login')
    }, 3000)
  } else {
    error.value = result.message
  }
}

// 跳转到登录页
const goToLogin = () => {
  router.push('/login')
}
</script>

<template>
  <div class="forgot-password-container">
    <div class="forgot-password-form">
      <h2>重置密码</h2>
      <form @submit="handleResetPassword">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            id="username"
            type="text"
            v-model="username"
            placeholder="请输入您的用户名"
            class="form-input"
          />
        </div>
        <div class="form-group">
          <label for="newPassword">新密码</label>
          <input
            id="newPassword"
            type="password"
            v-model="newPassword"
            placeholder="请输入新密码（至少6个字符）"
            class="form-input"
          />
        </div>
        <div class="form-group">
          <label for="confirmNewPassword">确认新密码</label>
          <input
            id="confirmNewPassword"
            type="password"
            v-model="confirmNewPassword"
            placeholder="请再次输入新密码"
            class="form-input"
          />
        </div>
        <div v-if="error" class="error-message">{{ error }}</div>
        <div v-if="success" class="success-message">{{ success }}，即将跳转到登录页面...</div>
        <button type="submit" class="reset-button">重置密码</button>
      </form>
      <div class="login-link">
        记得密码了？<button @click="goToLogin" class="link-button">立即登录</button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.forgot-password-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f5f5;
}

.forgot-password-form {
  background-color: white;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

.forgot-password-form h2 {
  text-align: center;
  margin-bottom: 1.5rem;
  color: #333;
}

.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 500;
  color: #555;
}

.form-input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
  transition: border-color 0.3s;
}

.form-input:focus {
  outline: none;
  border-color: #42b983;
}

.error-message {
  color: #ff4d4f;
  margin-bottom: 1rem;
  font-size: 0.9rem;
}

.success-message {
  color: #52c41a;
  margin-bottom: 1rem;
  font-size: 0.9rem;
}

.reset-button {
  width: 100%;
  padding: 0.75rem;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
  transition: background-color 0.3s;
  margin-bottom: 1rem;
}

.reset-button:hover {
  background-color: #3aa675;
}

.login-link {
  text-align: center;
  margin-top: 1rem;
  color: #666;
}

.link-button {
  background: none;
  border: none;
  color: #42b983;
  cursor: pointer;
  font-size: inherit;
  padding: 0;
  text-decoration: underline;
}

.link-button:hover {
  color: #3aa675;
}
</style>